﻿using System.Collections.Generic;
using Red.Controls.TimeLine.Commands;
using Red.Controls.TimeLine.Core;
using Red.Controls.ViewModels;

namespace Red.Controls.TimeLine.ViewModels
{
    public class PointTrackEvent : TrackItemBase
    {
        #region C-tor

        public PointTrackEvent( TimeLineId id, TimeLineControler controler )
            : base( id, controler )
        {
        }

        #endregion

        #region Properites

        public override IEnumerable<RedMenuDataItem> Menu
        {
            get
            {
                List<RedMenuDataItem> menu = new List<RedMenuDataItem>();                
                menu.Add( new RedMenuDataItem( new CommandWrapper( TimeLineCommands.DeleteEvent, this ) ) );

                return menu;
            }
        }

        #endregion
    }

    public class DurationTrackEvent : TrackItemBase
    {
        #region Data

        private double _endTime = 0.0;
        private double _durationScreenWidth = 0.0;

        #endregion

        #region C-tor

        public DurationTrackEvent( TimeLineId id, TimeLineControler controler )
            : base( id, controler )
        {
        }

        #endregion

        #region Properties

        public override IEnumerable<RedMenuDataItem> Menu
        {
            get
            {
                List<RedMenuDataItem> menu = new List<RedMenuDataItem>();
                menu.Add( new RedMenuDataItem( new CommandWrapper( TimeLineCommands.DeleteEvent, this ) ) );

                return menu;
            }
        }        

        public double EndTime
        {
            get { return _endTime; }           
            private set
            {
                if ( _endTime != value )
                {
                    _endTime = value;
                    OnPropertyChanged();
                }
            }
        }

        public double DurationTime
        {
            get { return DataModel?.GetValue<double>( "DurationTime" ) ?? 0.0; }
            set
            {
                double time = TimeLineControler.SnapTime( value );
                if ( DataModel != null )
                {
                    DataModel.SetValue<double>( "DurationTime", time );
                }
            }
        }

        public double DurationScreenWidth
        {
            get { return _durationScreenWidth; }
            private set
            {
                if ( _durationScreenWidth != value )
                {
                    _durationScreenWidth = value;
                    OnPropertyChanged();
                }
            }
        }
     
        #endregion

        #region Public Methods

        public override void RecalculateVisualsProperties()
        {
            DurationScreenWidth = TimeLineControler.TimeToPixelRatio * DurationTime;
        }

        #endregion

        #region Protected Methods

        protected override void OnDataModelChanged()
        {
            base.OnDataModelChanged();
            DataModel.RegisterProperty( "DurationTime", "durationTime", OnDurationTimeChanged );
        }

        private void OnDurationTimeChanged( string propertyName, string value )
        {
            RecalculateVisualsProperties();
            RecalculateEndTime();

            OnPropertyChanged( propertyName );
        }

        protected override void OnActionTimeChanged( string propertyName, string value )
        {
            RecalculateEndTime();

            OnPropertyChanged( propertyName );
        }

        #endregion

        #region Private Methods

        private void RecalculateEndTime()
        {
            EndTime = ActionTime + DurationTime;
        }

        #endregion
    }
}
